/*
 * main.c
 *
 *  Created on: 14.10.2012
 *      Author: Eugen Schneider
 */
#include <stdio.h>
#include <strings.h>
#include <math.h>

#include "../header/floatingGen.h"

void printInvalid() {

	printf("\nInput invalid!\nEnter again: ");
}

int main(int argc, char **argv) {

	struct flPointNumDouble number;
	int input;

	printf(
			"\n\n\n"
					"=======================================================================================\n"
					"=======================================================================================\n"
					"Floating Point Numbers Generator\n"
					"=======================================================================================\n"
					"=======================================================================================\n");

	printf("\nEnter the base: ");
	scanf("%i", &input);

	while (input <= 0 || input >= 257) {

		printInvalid();

		scanf("%i", &input);

	}
	number.base = (uint8_t) input;

	printf("\nEnter the mantissa length: ");
	scanf("%i", &input);
	number.mantissaLenght = (uint8_t) input;

	while (number.mantissaLenght <= 0 || number.mantissaLenght >= 54) {

		printInvalid();
		scanf("%i", &input);
		number.mantissaLenght = (uint8_t) input;

	}

	printf("\nEnter the least exponent: ");
	scanf("%i", &input);
	number.minExponent = (int8_t) input;

	while (number.minExponent >= 1024 || number.minExponent <= -1021) {

		printInvalid();
		scanf("%i", &input);
		number.minExponent = (int8_t) input;

	}

	printf("\nEnter the maximum exponent: ");
	scanf("%i", &input);
	number.maxExponent = (uint8_t) input;

	while (number.maxExponent <= number.minExponent
			|| number.maxExponent >= 1024) {

		printInvalid();
		scanf("%i", &input);
		number.maxExponent = (uint8_t) input;

	}

	printf("\nEnter the  exponent: ");
	scanf("%i", &input);
	number.exponent = (int32_t) input;

	while (number.exponent <= number.minExponent || number.maxExponent >= 1024) {

		printInvalid();
		scanf("%i", &input);
		number.exponent = (uint32_t) input;

	}

//	bool flag = false;
	char sign;
	printf("\nEnter the sign: ");

	scanf("%c", &sign);

	while ((sign != '+') && (sign != '-')) {

		printInvalid();
		scanf("%c", &sign);

	}
	if (sign == '+') {

		number.sign = 1;
	} else {
		number.sign = -1;
	}

	printf("\nEnter the mantissa: ");
	scanf("%i", &input);
	number.mantissa = (uint64_t) input;

	while (number.mantissa < 0) {

		printInvalid();
		scanf("%i", &input);
		number.mantissa = (uint64_t) input;

	}

	printf("\n\nThe decimal value is: %f",
			number.sign
					* (number.mantissa
							* (pow((double) number.base,
									(double) number.exponent))));
	printf(
			"\n\n\n"
					"=======================================================================================\n"
					"=======================================================================================\n"
					"End of Floating Point Numbers Generator\n"
					"=======================================================================================\n"
					"=======================================================================================\n");

//	printf(
//			"\n\n Entered values\nBase: %i\nMantissa length: %i\nmin Exponent: %i\nmax Exponent: %i\n",
//			number.base, number.mantissaLenght, number.minExponent,
//			number.maxExponent);

	return 0;
}

